package com.smashcrush.client.gwt;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.MenuBar.MenuBarImages;

public class Header extends Composite {

	Workspace workspace;

	public Header(final Workspace workspace) {

		this.workspace = workspace;

		Images im = GWT.create(Images.class);

		Grid grid = new Grid(2, 1);
		grid.setWidth("100%");

		grid.getRowFormatter().getElement(0).setId("header");
		grid.getRowFormatter().getElement(1).setId("buttonsMenu");

		FlexTable table = new FlexTable();
		grid.setWidget(0, 0, table);
		table.setWidget(0, 0, im.logo().createImage());
		table.setText(0, 1, "");
		table.setText(0, 2, "Settings |");
		table.setText(0, 3, "Logout");

		table.getFlexCellFormatter().setWidth(0, 0, "5%");
		table.getFlexCellFormatter().setWidth(0, 1, "85%");
		table.getFlexCellFormatter().setWidth(0, 2, "5%");
		table.getFlexCellFormatter().setWidth(0, 3, "5%");

		// Create a menu bar
		MenuBar menu = new MenuBar();
		menu.setAutoOpen(true);
		menu.setWidth("100%");
		menu.setAnimationEnabled(true);

		// Create a sub menu of recent documents
		// MenuBar recentDocsMenu = new MenuBar(true);
		// String[] recentDocs = constants.cwMenuBarFileRecents();
		// for (int i = 0; i < recentDocs.length; i++) {
		// recentDocsMenu.addItem(recentDocs[i], menuCommand);
		// }

		// Create the file menu
		MenuBar fileMenu = new MenuBar(true);
		fileMenu.setAnimationEnabled(true);
		menu.addItem(new MenuItem("Scripts", fileMenu));

		MenuItem newScript = new MenuItem("New Script", new Command() {

			@Override
			public void execute() {
				workspace.newScript();
			}
		});
		fileMenu.addItem(newScript);

		MenuItem openScript = new MenuItem("Open Script", new Command() {

			@Override
			public void execute() {
				workspace.showScripts();
			}
		});
		fileMenu.addItem(openScript);

		MenuItem runningsScripts = new MenuItem("Running Scripts",
				new Command() {

					@Override
					public void execute() {
						workspace.showScripts();
					}
				});
		fileMenu.addItem(runningsScripts);

		// HorizontalPanel flowPanel = new HorizontalPanel();
		// flowPanel.setWidth("100%");
		// flowPanel.setStylePrimaryName("buttonsMenu");
		grid.setWidget(1, 0, menu);

		// Create a menu bar
		//
		// ToolButton pushButton1 = new ToolButton("<div>asfd</div>", new
		// ClickHandler() {
		//			
		// @Override
		// public void onClick(ClickEvent event) {
		// // TODO Auto-generated method stub
		//				
		// }
		// });
		// flowPanel.add(pushButton1);
		//		
		// System.out.println(pushButton1);
		//		
		// flowPanel.add(createItem("Scripts", new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// workspace.showScripts();
		// }
		// }));
		//
		// flowPanel.add(createItem("Reports", new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// workspace.showScripts();
		// }
		// }));
		//
		// flowPanel.add(createItem("Settings", new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// workspace.showScripts();
		// }
		// }));

		// MenuBar menu = new MenuBar(true);
		// menu.
		// grid.setWidget(1, 0, menu);
		// // menu.setAutoOpen(true);
		// menu.setWidth("100%");
		// menu.setAnimationEnabled(true);
		//
		// // Create the file menu
		// MenuBar fileMenu = new MenuBar(true);
		// // fileMenu.setAnimationEnabled(true);
		// menu.addItem(new MenuItem("Scripts " + im.scripts().getHTML(), true,
		// fileMenu));
		//	    

		initWidget(grid);
	}

	private PushButton createItem(String text, ClickHandler clickHandler) {
		PushButton scripts = new PushButton(text, clickHandler);
		scripts.setSize("150px", "25px");
		return scripts;
	}

	public interface Images extends MenuBarImages {
		AbstractImagePrototype scripts();

		AbstractImagePrototype jobs();

		AbstractImagePrototype settings();

		AbstractImagePrototype logo();
		

		/**
		 * Use noimage.png, which is a blank 1x1 image.
		 */
		@Resource("noimage.png")
		AbstractImagePrototype treeLeaf();
	}

}
